{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "966ee9fe",
   "metadata": {},
   "source": [
    ">上节课知识点回顾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "9ba67d8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f5cbfd5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.输入我们需要的API网站注册的API_Key\n",
    "API_Key = \"4m-swMSCwlTnjOut1bpvDHqkmkLrCBcI\"\n",
    "API_secret = \"fqn8e-gsWkLNt3dOVSTYmsxOj3aoOihy\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "04c8ba61",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 3.准备 AI测试接口的url\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5a1af324",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4. 调用方法 POST"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "badc2b3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5. 准备ur1传递参数\n",
    "payload = {\n",
    "     \"api_key\":API_Key,\n",
    "     \"api_secret\":API_secret,\n",
    "     \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftx28%2F590423182256024.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649904557&t=c57b6291317e2a7be27c53dcdb8ea0a8\",\n",
    "     \"return_attributes\": \"gender,age,smiling\"\n",
    "\n",
    " }\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "98496647",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 6.发起请求\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "05c26850",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "80a98280",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648466960,5bf940b1-e4dd-4295-90cd-3e6404b20860',\n",
       " 'time_used': 194,\n",
       " 'faces': [{'face_token': 'ef42501f416974c96f8701f68d2b24a5',\n",
       "   'face_rectangle': {'top': 157, 'left': 57, 'width': 226, 'height': 226},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 3},\n",
       "    'smile': {'value': 100.0, 'threshold': 50.0}}}],\n",
       " 'image_id': '5CfTU2cROD9pATuDRDPQfA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f747980",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "06a0f421",
   "metadata": {},
   "source": [
    "# 1.人脸分析API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2f505653",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "Analyze_base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '4m-swMSCwlTnjOut1bpvDHqkmkLrCBcI'\n",
    "API_secret = 'fqn8e-gsWkLNt3dOVSTYmsxOj3aoOihy'\n",
    "\n",
    "# 3. POST\n",
    "\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API。\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用。\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':'ea6d199b4ad58200a01b2a2579103714',\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus',\n",
    "    'beauty_score_min':0,\n",
    "    'beauty_score_max':100\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r_analyze = requests.post(url = Analyze_base_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "69d88e6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [400]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "91ba5cf1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648467068,289c8ea0-e0c3-464f-8eef-827f51c33a7a',\n",
       " 'time_used': 56,\n",
       " 'error_message': 'MISSING_ARGUMENTS: image_url, image_file, image_base64'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_analyze.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d7ca36e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "43c84482",
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'faces'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-17767aa2310d>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson_normalize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mr_analyze\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'faces'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'faces'"
     ]
    }
   ],
   "source": [
    "pd.json_normalize(r_analyze.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bf16e24",
   "metadata": {},
   "source": [
    "##### 以上？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "818d748d",
   "metadata": {},
   "source": [
    "# 2.人脸识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "05b6adfc",
   "metadata": {},
   "outputs": [],
   "source": [
    "？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ae7d242",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = \"B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI\"\n",
    "API_secret = \"1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI\"\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "payload ={\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_secret,\n",
    "    \"image_url\":\"https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202007%2F08%2F20200708192538_jBxPt.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649907196&t=1b97f83f0bb4618c0d1d187f4a916cb9\",\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "541bba10",
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72f60af5",
   "metadata": {},
   "outputs": [],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb58875a",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "213c0404",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url \n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/compare\"\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI'\n",
    "API_sercret = '1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI'\n",
    "\n",
    "# 3 POST\n",
    "\n",
    "# 4. 权限：所有 API Key 都可以调用本 API\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens1':'fd914700794abd4430a3eff1b1a88405',\n",
    "    'face_tokens2':'a6f0c2aa9c21306b237d25c411535abf',\n",
    "    \n",
    "}\n",
    "\n",
    "# 6. requests\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "74748687",
   "metadata": {},
   "outputs": [],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "767206bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "decdcc5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 打开本地图片\n",
    "from PIL import Image\n",
    "Image.open('Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c91391a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 打开本地图片二进制\n",
    "print(open('Liu_01.jpeg', 'rb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14768f0a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "import requests\n",
    "API_key = 'jRpCaZ34kqNYJ8Zppdc-yGGum_YkETov'\n",
    "API_sercret = 'Vr0PtRCw-ZFwYXTKVfi6aDNaSqlfunK3'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2c1abe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "def face_detect(API_key,API_sercret,image_path):\n",
    "    \"\"\"该函数为调用face++ face_detect接口\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    files = {'image_file': ('Liu_01.jpeg', open('Liu_01.jpeg', 'rb'), 'multipart/form-data')}\n",
    "    payload = {\n",
    "            'api_key':API_key,\n",
    "            'api_secret':API_sercret\n",
    "            }\n",
    "    \n",
    "\n",
    "    r = requests.post(base_url, params = payload,files = files )\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d17eefc8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试\n",
    "face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b453c95d",
   "metadata": {},
   "source": [
    "### 准备人脸元数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be0d7842",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "888bbacd",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_meta_data = face_detect(API_key,API_sercret,'Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "362b2f75",
   "metadata": {},
   "outputs": [],
   "source": [
    "Liu_meta_data_face_token = Liu_meta_data['faces'][0]['face_token']\n",
    "Liu_meta_data_face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e80babc",
   "metadata": {},
   "source": [
    "### 给出人脸图片进行相似度查询"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "addc44e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. 准备测试人脸图片 face2\n",
    "\n",
    "face_detect(API_key,API_sercret,'Liu_02.jpeg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c976fb05",
   "metadata": {},
   "outputs": [],
   "source": [
    "face02_token = face_detect(API_key,API_sercret,'Liu_02.jpeg')['faces'][0]['face_token']\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "875c13bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2. 人脸对比\n",
    "\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':Liu_meta_data_face_token,\n",
    "    'face_token2':face02_token\n",
    "}\n",
    "r = requests.post(compare_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6982624",
   "metadata": {},
   "outputs": [],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c1e72d8",
   "metadata": {},
   "source": [
    "# 3.封装应用函数（内部使用的接口）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "33ba3fc7",
   "metadata": {},
   "source": [
    "## 1.人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50ceb651",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7b286e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "API_key = 'B2bd2_Ym1FYWz4JvEAgT8nuYDTGONyRI'\n",
    "API_sercret = '1YUNd3VkSybl-_vOPwTU7PDWsEL5PoyI'\n",
    "img_url_01 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fblog%2F202108%2F20%2F20210820101310_7952f.thumb.1000_0.jpg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1649903284&t=dc2b191f83e7db1a4309e43ac878f883'\n",
    "img_url_02 = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F202004%2F07%2F20200407174156_2Svds.thumb.1000_0.jpeg&refer=http%3A%2F%2Fc-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1650459946&t=f7c8a181b21e9d4e72cebb5f10c25816'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3a30ee3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d20cc543",
   "metadata": {},
   "outputs": [],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cac5cc76",
   "metadata": {},
   "outputs": [],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f3c99c0b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c96fcd04",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd42dc7c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9767caeb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28c4de9a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
